package top.zhangjianyong.test.reptiles;

import cn.hutool.core.collection.CollectionUtil;
import lombok.extern.slf4j.Slf4j;
import top.zhangjianyong.utils.JDBCUtil;
import top.zhangjianyong.utils.UrlUtil;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author ： zhangjianyong
 * @date ： 2023/4/18 4:51 PM
 */
@Slf4j
public class UpdateHostMain {

    public static void init() throws SQLException {
        String host = "rm-bp11xb28f141i1808ho.rwlb.rds.aliyuncs.com";
        String port = "3306";
        String dbname = "zhangjianyong_tools";
        String url = "jdbc:mysql://" + host + ":" + port + "/" + dbname + "?useSSL=false&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Hongkong";
        String user = "root";
        String password = "zhangjianyong@2023";
        JDBCUtil.init(url, user, password);
    }



    public static void main(String[] args) throws SQLException {
        init();

        Set<String> hosts = new HashSet<String>(
                Arrays.asList(new String(
                        "com.cn|edu.cn|gov.cn|net.cn|org.cn|com.hk|com.hk|org.hk|com.se|org.se")
                        .split("\\|")));
       hosts= hosts.stream().map(host-> UrlUtil.reveres(host)).collect(Collectors.toSet());

        for (String host : hosts) {
            update(host);
        }


    }

    public static void update(String host) throws SQLException {
        int count = 1;
        while (updateHost("reptile_url",host)) {
            log.info("reptile_url:第{}次更新完成", count++);
        }
        count = 1;
        while (updateHost("reptile_url_err",host)) {
            log.info("reptile_url_err:第{}次更新完成", count++);
        }
        count = 1;
        while (updateHost("reptile_url_suc",host)) {
            log.info("reptile_url_suc:第{}次更新完成", count++);
        }
        count = 1;
        while (updateHost("reptile_url_wait",host)) {
            log.info("reptile_url_wait:第{}次更新完成", count++);
        }
    }

    public static boolean delHost() throws SQLException {
        int update = JDBCUtil.update("update reptile_url set `host`= null where `host` is not null limit 10000 ");
        //JDBCUtil.commit();
        if (update == 0) return false;
        return true;
    }

    public static boolean updateHost(String table, String sHost) throws SQLException {
        List<Map<String, Object>> maps = JDBCUtil.qryList("SELECT id,url  from " + table + " where `host` =? limit 1000", sHost);
        if (CollectionUtil.isEmpty(maps)) {
            return false;
        }
        PreparedStatement preparedStatement = JDBCUtil.getConnection().prepareStatement("update " + table + " set `host` =? where `id` = ?");
        for (Map<String, Object> map : maps) {
            Long id = (Long) map.get("id");
            String url = (String) map.get("url");
            url=url.toLowerCase();
            log.info("批量更新：{}-{}", id, url);
            String host = UrlUtil.reveresHost(url);
            preparedStatement.setObject(1, host);
            preparedStatement.setObject(2, id);
            log.info("host：{}", host);
            preparedStatement.addBatch();
        }
        preparedStatement.executeBatch();
        JDBCUtil.close(preparedStatement);
        //JDBCUtil.commit();
        log.info("提交更新");
        return true;
    }
}
